package com.xxxx.server.controller;


import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.Menu;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.service.IMenuRoleService;
import com.xxxx.server.service.IMenuService;
import com.xxxx.server.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-08-17
 */
@RestController
@RequestMapping("/system/basic")
public class RoleController {

    @Resource
    private IRoleService roleService;

    @Resource
    private IMenuService menuService;

    @Resource
    private IMenuRoleService menuRoleService;

    /**
     * 初始化所有角色
     */
    @GetMapping("/permiss")
    @ApiOperation(value = "初始化用户及用户角色")
    public List<Role> initAllRoles(){

        return roleService.list();
    }

    /**
     * 初始化所有菜单
     */
    @GetMapping("/permiss/menus")
    @ApiOperation(value = "初始化所有菜单")
    public List<Menu> initAllMenus(){

        return menuService.queryAllMenus();
    }

    /**
     *根据角色id查菜单id
     */
    @GetMapping("/permiss/mid/{rId}")
    @ApiOperation(value = "根据角色id查菜单id")
    public List<Integer> initSelectedMenus(@PathVariable Integer rId){

        return menuService.initSelectedMenus(rId);
    }

    /**
     * 修改用户拥有的权限
     */
    @PutMapping("/permiss")
    @ApiOperation(value = "修改用户拥有的权限")
    public RespBean updatePermiss(Integer rid, String mids){  // 3,   rids = 3,4,2

        menuRoleService.updateAdminRole(rid, mids);

        return RespBean.success("更新成功！");
    }

    /**
     * 添加角色
     */
    @PostMapping("/permiss/role")
    @ApiOperation(value = "添加角色")
    public RespBean addRole(@RequestBody Role role){

        roleService.save(role);

        return RespBean.success("添加成功");
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/permiss/role/{id}")
    @ApiOperation(value = "删除角色")
    public RespBean deleteRole(@PathVariable Integer id){

        roleService.removeById(id);

        return RespBean.success("删除成功");
    }

}
